import { Provide } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';

import {
  CreateCustomerDto,
  UpdateCustomerDto,
  CustomerPageDto,
} from '../dto/customer';

import { CustomerEntity } from '../entity/customer';

@Provide()
export class CustomerService {
  @InjectEntityModel(CustomerEntity)
  customerEntity: Repository<CustomerEntity>;

  async add(customer: CreateCustomerDto) {
    return await this.customerEntity.save(customer);
  }

  async delete(customerId: number) {
    return await this.customerEntity.delete(customerId);
  }

  async update(customer: UpdateCustomerDto) {
    return await this.customerEntity.update(customer.id, customer);
  }

  async info(customerId: number) {
    return await this.customerEntity.findOne(customerId);
  }

  async page(pageOp: CustomerPageDto) {
    return await this.customerEntity.find({
      where: { id: pageOp.id },
      skip: pageOp.start,
      take: pageOp.limit,
      order: {
        updateTime: 'DESC',
      },
    });
  }
}
